<?php

namespace app\common\service\SystemRabbitMqService\MessageService;
use app\common\service\SystemRabbitMqService\AmqpMessageManager;

/**
 * Bbc商品搜索服务es
 * @author linan
 */
class GoodsElasticSearchMessageService extends AmqpMessageManager
{
    const NAME = 'YC_SYNC_BBC_RECEIVE_LIST';
    protected $queueName = self::NAME,
    $routingKey = self::NAME,
    $exchangeName = self::NAME,
    $isReject = true,
    $consumer_title = "Bbc商品搜索服务es";
    protected $mq = '';
    public function __construct( string $mq = null )
    {
        $this->mq = $mq;
        parent::__construct($this->pushKeyMapping[$mq]);
    }
    
    /**
     * 推送消息映射
     * @var array
     */
    public $pushKeyMapping = [
        'channel'   => 'document_qqc_qqccommodity',
        'store'     => 'document_qd_qdcommodity',
    ];
    
    /**
     * 推送渠道或者店铺商品到搜索服务
     * @param array $data
     * @param string $type
     * @param string $method
     */
    public function pushNew(array $data,string $method)
    {
        $actionType = $method == 'delete' ? 0 : 1;
        $num=count($data);
        if($num>100){
            $flag=true;
            $i=0;
            while ($flag){
                $new=array_slice($data,$i,100);
                if($new){
                    $message = ['actionType'=>$actionType,'datas'=>$new];
                    $result = (new GoodsElasticSearchMessageService($this->mq))->produce($message);
                    print_r($result);
                    $i+=100;
                }else{
                    $flag=false;
                }
            }
        }else{
            $message = ['actionType'=>$actionType,'datas'=>$data];
            $result = (new GoodsElasticSearchMessageService($this->mq))->produce($message);
        }
        return  ['msg'=>'ok'];
    }
}